<?php

namespace App\Http\Middleware;

use Closure;
use App\Models\SystemManagement\Privilege;
use App\Models\SystemManagement\OperationLog as Log;

class OperationLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $request_path = $request->path();
        $param = substr($request_path,strripos($request_path,"/")+1);
        if(is_numeric($param)){
            $path = substr($request_path, 4, strrpos($request_path,'/')-4);
        }else{
            $path = substr($request_path, 4);
        }
        $request_method = strtolower($request->method());
        // dd($path);
        if($request_method != 'get'){
            $action = Privilege::where(['method'=>$request_method,'route'=>$path])->first('name');
            $action = $action['name'] ? $action['name'] : '未知操作';
            Log::writeData($request_method,$request_path,$action);
        }
        return $next($request);
    }
}
